package or;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class T0815 {

    public int numBusesToDestination(int[][] routes, int source, int target) {
        int len=0;
        for(int [] route:routes){
            for (int rou:route){
                len=Math.max(len,rou);
            }
        }
        if(target>len){
            return -1;
        }
        int result=0;
        int [] dp=new int[len+1];
        Arrays.fill(dp,len+1);
        dp[source]=0;
        boolean b=true;
        while(b){
            for(int [] route:routes){
                int min=len+1;
                for (int rou:route){
                    min=Math.min(min,dp[rou]);
                }
                for (int rou:route){
                    if (dp[rou]>min+1){
                        dp[rou]=min+1;
                        b=false;
                    }
                }
            }
            if (dp[target]!=len+1) {
                return dp[target];
            }
            b= !b;
        }
        if (dp[target]!=len+1) {
            return dp[target];
        }
        return -1;


    }

    @Test
    public void Test(){


        int[][] routes={{1,2,7},{3,6,7}};


        int r=numBusesToDestination(routes,1,6);
        System.out.println(r);
    }
}
